package org.jeecg.modules.business.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.DecimalMin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hpsf.Decimal;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.dto.CouponsGenerateDto;
import org.jeecg.modules.business.dto.PostDataDto;
import org.jeecg.modules.business.dto.ProvideCouponsUsedDto;
import org.jeecg.modules.business.entity.BusMarketCoupons;
import org.jeecg.modules.business.entity.BusMarketCouponsCashUsed;
import org.jeecg.modules.business.entity.BusMarketCouponsUsed;
import org.jeecg.modules.business.entity.BusMarketMealCouponsUsed;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.service.IBusMarketCouponsCashUsedService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.business.service.IBusMarketCouponsService;
import org.jeecg.modules.business.service.IBusMarketCouponsUsedService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: bus_market_coupons_cash_used
 * @Author: jeecg-boot
 * @Date:   2023-03-11
 * @Version: V1.0
 */
@Api(tags="bus_market_coupons_cash_used")
@RestController
@RequestMapping("/business/busMarketCouponsCashUsed")
@Slf4j
public class BusMarketCouponsCashUsedController extends JeecgController<BusMarketCouponsCashUsed, IBusMarketCouponsCashUsedService> {
	@Autowired
	private IBusMarketCouponsCashUsedService busMarketCouponsCashUsedService;
	 @Autowired
	 private IBusMarketCouponsService busMarketCouponsService;
	/**
	 * 分页列表查询
	 *
	 * @param busMarketCouponsCashUsed
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bus_market_coupons_cash_used-分页列表查询")
	@ApiOperation(value="bus_market_coupons_cash_used-分页列表查询", notes="bus_market_coupons_cash_used-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BusMarketCouponsCashUsed>> queryPageList(BusMarketCouponsCashUsed busMarketCouponsCashUsed,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusMarketCouponsCashUsed> queryWrapper = QueryGenerator.initQueryWrapper(busMarketCouponsCashUsed, req.getParameterMap());
		Page<BusMarketCouponsCashUsed> page = new Page<BusMarketCouponsCashUsed>(pageNo, pageSize);
		IPage<BusMarketCouponsCashUsed> pageList = busMarketCouponsCashUsedService.page(page, queryWrapper);
		pageList.getRecords().forEach(item -> {
			BusMarketCoupons coupons = busMarketCouponsService.getById(item.getCouponsId());
			if (coupons != null) {
				item.setConponsName(coupons.getName());
			}
			CouponsStatusEnum couponsStatusEnum = CouponsStatusEnum.val(item.getStatus());
			if (couponsStatusEnum != null) {
				item.setStatusName(couponsStatusEnum.getTitle());
			}
		});
		return Result.OK(pageList);
	}

	 /**
	  * 获取可用优惠券
	  * @param pageNo
	  * @param pageSize
	  * @param hotelId
	  * @param conditions 满多少金额
	  * @param mobile 手机号
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="获取可用优惠券", notes="获取可用优惠券")
	 @GetMapping(value = "/memeberCouponList")
	 public Result<IPage<BusMarketMealCouponsUsed>> memeberCouponList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																   @RequestParam(name="hotelId", required = true) String hotelId,
																	  @RequestParam(name="conditions", required = true) @JsonFormat() BigDecimal conditions,
																   @RequestParam(name="mobile", required = true) String mobile,
																   HttpServletRequest req) {
		 Page<BusMarketMealCouponsUsed> page = new Page<BusMarketMealCouponsUsed>(pageNo, pageSize);
		 IPage<BusMarketMealCouponsUsed> pageList = busMarketCouponsCashUsedService.memeberCouponList(page, hotelId, conditions, mobile);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param busMarketCouponsCashUsed
	 * @return
	 */
	@AutoLog(value = "bus_market_coupons_cash_used-添加")
	@ApiOperation(value="bus_market_coupons_cash_used-添加", notes="bus_market_coupons_cash_used-添加")
	//@RequiresPermissions("business:bus_market_coupons_cash_used:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BusMarketCouponsCashUsed busMarketCouponsCashUsed) {
		busMarketCouponsCashUsedService.save(busMarketCouponsCashUsed);
		return Result.OK("添加成功！");
	}

	 /**
	  *   生成优惠券
	  *
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "优惠券领取使用表-生成")
	 @ApiOperation(value="优惠券领取使用表-生成", notes="优惠券领取使用表-生成")
	 @PostMapping(value = "/generate")
	 public Result<String> generate(@RequestBody CouponsGenerateDto dto) {
		 Result res = busMarketCouponsCashUsedService.generate(dto);
		 return res;
	 }

	 /**
	  *   优惠券发放给非会员
	  *
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "优惠券领取使用表-优惠券发放给非会员")
	 @ApiOperation(value="优惠券领取使用表-优惠券发放给非会员", notes="优惠券领取使用表-优惠券发放给非会员")
	 @PostMapping(value = "/gainCouponsCashUsedToNoMember")
	 public Result<String> gainCouponsCashUsedToNoMember(@RequestBody ProvideCouponsUsedDto dto) {
		 Result res = busMarketCouponsCashUsedService.gainCouponsCashUsedToNoMember(dto);
		 return res;
	 }

	 /**
	  *   优惠券发放给会员
	  *
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "优惠券领取使用表-优惠券发放给会员")
	 @ApiOperation(value="优惠券领取使用表-优惠券发放给会员", notes="优惠券领取使用表-优惠券发放给会员")
	 @PostMapping(value = "/gainCouponsCashUsedToMember")
	 public Result<String> gainCouponsCashUsedToMember(@RequestBody ProvideCouponsUsedDto dto) {
		 Result res = busMarketCouponsCashUsedService.gainCouponsCashUsedToMember(dto);
		 return res;
	 }

	 /**
	  * 作废
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "优惠券领取使用表-作废")
	 @ApiOperation(value="优惠券领取使用表-作废", notes="优惠券领取使用表-作废")
	 @RequestMapping(value = "/voided", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> voided(@RequestBody PostDataDto dto) {
		 LambdaUpdateWrapper<BusMarketCouponsCashUsed> updateWrapper = new UpdateWrapper().lambda();
		 updateWrapper.set(BusMarketCouponsCashUsed::getStatus, CouponsStatusEnum.voided.getKey());
		 updateWrapper.eq(BusMarketCouponsCashUsed::getId, dto.getId());
		 busMarketCouponsCashUsedService.update(updateWrapper);
		 return Result.OK("编辑成功!");
	 }
	
	/**
	 *  编辑
	 *
	 * @param busMarketCouponsCashUsed
	 * @return
	 */
	@AutoLog(value = "bus_market_coupons_cash_used-编辑")
	@ApiOperation(value="bus_market_coupons_cash_used-编辑", notes="bus_market_coupons_cash_used-编辑")
	//@RequiresPermissions("business:bus_market_coupons_cash_used:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BusMarketCouponsCashUsed busMarketCouponsCashUsed) {
		busMarketCouponsCashUsedService.updateById(busMarketCouponsCashUsed);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "bus_market_coupons_cash_used-通过id删除")
	@ApiOperation(value="bus_market_coupons_cash_used-通过id删除", notes="bus_market_coupons_cash_used-通过id删除")
	//@RequiresPermissions("business:bus_market_coupons_cash_used:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		busMarketCouponsCashUsedService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "bus_market_coupons_cash_used-批量删除")
	@ApiOperation(value="bus_market_coupons_cash_used-批量删除", notes="bus_market_coupons_cash_used-批量删除")
	//@RequiresPermissions("business:bus_market_coupons_cash_used:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busMarketCouponsCashUsedService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bus_market_coupons_cash_used-通过id查询")
	@ApiOperation(value="bus_market_coupons_cash_used-通过id查询", notes="bus_market_coupons_cash_used-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BusMarketCouponsCashUsed> queryById(@RequestParam(name="id",required=true) String id) {
		BusMarketCouponsCashUsed busMarketCouponsCashUsed = busMarketCouponsCashUsedService.getById(id);
		if(busMarketCouponsCashUsed==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(busMarketCouponsCashUsed);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busMarketCouponsCashUsed
    */
    //@RequiresPermissions("business:bus_market_coupons_cash_used:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusMarketCouponsCashUsed busMarketCouponsCashUsed) {
        return super.exportXls(request, busMarketCouponsCashUsed, BusMarketCouponsCashUsed.class, "bus_market_coupons_cash_used");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("business:bus_market_coupons_cash_used:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusMarketCouponsCashUsed.class);
    }

}
